package com.anis.project.medcare.search.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

public class NewAppointementDialog extends Dialog {

	private DateTime swtCalendar;
	private Text appText;
	int day;
	int month;
	int year;
	private Text rdvDecidedLabel;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */
	public NewAppointementDialog(Shell parentShell, Text appointement) {
		super(parentShell);
		this.appText = appointement;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1, false));

		Group calendarDaysGroup = new Group(container, SWT.NONE);
		GridData gd_calendarDaysGroup = new GridData(SWT.FILL, SWT.FILL, true,
				false, 1, 2);
		gd_calendarDaysGroup.heightHint = 204;
		calendarDaysGroup.setLayoutData(gd_calendarDaysGroup);
		calendarDaysGroup.setLayout(new GridLayout(2, false));

		swtCalendar = new DateTime(calendarDaysGroup, SWT.CALENDAR);
		GridData gd_swtCalendar = new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1);
		gd_swtCalendar.heightHint = 52;
		swtCalendar.setLayoutData(gd_swtCalendar);

		Group group = new Group(calendarDaysGroup, SWT.NONE);
		group.setLayout(new GridLayout(4, false));
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

		Label lblHours = new Label(group, SWT.NONE);
		lblHours.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,
				1, 1));
		lblHours.setText("Hours");
		new Label(group, SWT.NONE);

		Label lblMinutes = new Label(group, SWT.NONE);
		lblMinutes.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 1, 1));
		lblMinutes.setText("Minutes");
		new Label(group, SWT.NONE);

		Spinner spinner = new Spinner(group, SWT.BORDER);
		spinner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1,
				1));
		new Label(group, SWT.NONE);

		Spinner spinner_1 = new Spinner(group, SWT.BORDER);
		spinner_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,
				1, 1));

		Label lblNewLabel = new Label(group, SWT.NONE);

		rdvDecidedLabel = new Text(group, SWT.BORDER);
		rdvDecidedLabel.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				getOKButton().setEnabled(true);
			}
		});
		rdvDecidedLabel.setEditable(false);
		rdvDecidedLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 4, 1));

		swtCalendar.addMouseListener(new MouseListener() {

			@Override
			public void mouseUp(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				day = swtCalendar.getDay();
				month = swtCalendar.getMonth() + 1;
				year = swtCalendar.getYear();
				rdvDecidedLabel.setText(day + "-" + month + "-" + year);
			}
		});

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		getOKButton().setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}

	@Override
	protected void okPressed() {
		this.getShell().getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				appText.setText(year + "-" + month + "-" + day);
			}
		});

		super.okPressed();
	}
}
